var componentsReceptArray = new Array();
var userID = '';

function setEditMode(){
	if(document.getElementById('editbutton').value == 'редактиране'){
		document.getElementById('editbutton').value = 'айде';
		$(".table-submit").css("visibility","visible"); 
	} else {
		document.getElementById('editbutton').value = 'редактиране';
		$(".table-submit").css("visibility","hidden");
	}
}

function changeRcategory(catName){
	agent.call('','change_category','', catName);
	agent.call('','getRecepti','listReceptiCallback');
	generateCategries_o(null, catName, 'changeRcategory');
}

function listReceptiCallback(result){
	list_table_recepti(result, null);
}

function filterRecepti(){
	var filter = trim(document.getElementById("find").value.toString()).toLowerCase();
	var result = new Array();
	var j = 0;
	for(i=0; i<componentsReceptArray.length;i++){
		if (componentsReceptArray[i][2].toLowerCase().indexOf(filter) != -1 || componentsReceptArray[i][1].toLowerCase().indexOf(filter) != -1){
			result[j++] = new Array(componentsReceptArray[i][0],componentsReceptArray[i][1], componentsReceptArray[i][2], componentsReceptArray[i][3], componentsReceptArray[i][4]); 
		}
	}
	listRecepti(result);
}

function list_table_recepti(table, userid){
	if (userID != null){
		userID = userid;	
	}
	var listString = table+'';
	if (listString == ''){
		document.getElementById("maincontent").innerHTML = '';
		return;
	}
	componentsReceptArray = table;
	if(document.getElementById("find").value == ""){
		listRecepti(table);
	} else {
		filterRecepti();
	}
}

function listRecepti(table){
	var result =  '<div class="klas-za-padding"></div>';
	result += '<table class="list_recepti">';
	for(i=0; i<table.length;i++){
		result += '<tr>';
		result += '<td class="ime-rp" width="90%">';
		result += '<div>';
		result += '<a class="thickbox" class="ime-rp" href="recepta.php?iframe&idr='+table[i][0]+'">'+table[i][1]+'</a>';
		result += '</div>';
		result += '</td>';
		result += '<td class="remove-buton">';
		if (userID == table[i][4]) {
			result += '<form action="recepti.php" method="post">';
			result += '<input class="table-submit" type="submit" value="remove" name="Remove">';
			result += '<input type="hidden" name="id" value='+table[i]+'>';
			result += '</form>';
		}		
		result += '</td>';
		result += '</tr>';
	}
	result += '</table>';
	document.getElementById("maincontent").innerHTML = result;
}
